package db_main

import (
	"errors"
	"fmt"
	"gorm.io/gorm"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql"
	"longmen/server/pkg/db/mysql/models/models_main"
)

func GetPaymentApiParam(param map[string]interface{}) (*models_main.PaymentApi, error) {
	fName := "GetPaymentApiParam"
	o := &models_main.PaymentApi{}
	table := mysql.GetRTable(&models_main.PaymentApi{})
	mysql.Condition(table, param, "", "", 0, 0)
	err := table.Find(o).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}

func GetPaymentApisParam(param map[string]interface{}, order string, page, size int) ([]*models_main.PaymentApi, error) {
	fName := "GetPaymentApisParam"
	o := make([]*models_main.PaymentApi, 0)
	table := mysql.GetRTable(&models_main.PaymentApi{})
	mysql.Condition(table, param, "", order, page, size)
	err := table.Find(&o).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}

/**
 * @Author admin
 * @Description //查找到支付的API 接口
 * @version: v1.8.0
 * @Date 18:06 2022/8/18
 * @Param
 * @return
 **/
func GetPaymentApi(apiCode string) *models_main.PaymentApi {
	obj := &models_main.PaymentApi{}
	mysql.GetRTable(&models_main.PaymentApi{}).Where("code=?", apiCode).Find(&obj)
	return obj
}
